<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="设备号" prop="deviceImei">-->
<!--        <el-input-->
<!--          v-model="queryParams.deviceImei"-->
<!--          placeholder="请输入设备号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="监测时间">
        <el-date-picker
          v-model="daterangeNetworkTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
<!--      <el-form-item label="设备时间">-->
<!--        <el-date-picker-->
<!--          v-model="daterangeLocationTime"-->
<!--          style="width: 240px"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          type="daterange"-->
<!--          range-separator="-"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--        ></el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['deviceInfo:deviceInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['deviceInfo:deviceInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['deviceInfo:deviceInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['deviceInfo:deviceInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="编码" align="center" prop="id" />-->
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="设备号" align="center" prop="deviceImei" />-->
<!--      <el-table-column label="电池电压" align="center" prop="batteryLevel" />-->
      <el-table-column label="太阳能电池电压" align="center" prop="solarVoltage" />
      <el-table-column label="基站经度" align="center" prop="lbslocE" />
      <el-table-column label="基站纬度" align="center" prop="lbslocN" />
      <el-table-column label="监测时间" align="center" prop="networkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.networkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="设备时间" align="center" prop="locationTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.locationTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="含水率" align="center" prop="moistureContent" />
      <el-table-column label="温度" align="center" prop="temperature" />
      <el-table-column label="电导率 (uS/cm)" align="center" prop="electricalConductivity" />
      <el-table-column label="ph值" align="center" prop="ph" />
      <el-table-column label="氮含量" align="center" prop="nitrogenEstimate" />
      <el-table-column label="磷含量" align="center" prop="phosphorusEstimate" />
      <el-table-column label="钾含量" align="center" prop="potassiumEstimate" />
<!--      <el-table-column label="GPS定位经度" align="center" prop="gpslocE" />-->
<!--      <el-table-column label="GPS定位维度" align="center" prop="gpslocN" />-->
<!--      <el-table-column label="设备内存储的数据序号" align="center" prop="dataSeqid" />-->
      <el-table-column label="sim卡号" align="center" prop="deviceIccid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['deviceInfo:deviceInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['deviceInfo:deviceInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备号" prop="deviceImei">
          <el-input v-model="form.deviceImei" placeholder="请输入设备号" />
        </el-form-item>
        <el-form-item label="电池电压" prop="batteryLevel">
          <el-input v-model="form.batteryLevel" placeholder="请输入电池电压" />
        </el-form-item>
        <el-form-item label="太阳能电池电压" prop="solarVoltage">
          <el-input v-model="form.solarVoltage" placeholder="请输入太阳能电池电压" />
        </el-form-item>
        <el-form-item label="基站经度" prop="lbslocE">
          <el-input v-model="form.lbslocE" placeholder="请输入基站经度" />
        </el-form-item>
        <el-form-item label="基站纬度" prop="lbslocN">
          <el-input v-model="form.lbslocN" placeholder="请输入基站纬度" />
        </el-form-item>
        <el-form-item label="监测时间" prop="networkTime">
          <el-date-picker clearable
            v-model="form.networkTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择网络时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备时间" prop="locationTime">
          <el-date-picker clearable
            v-model="form.locationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择设备时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="含水率">
          <editor v-model="form.moistureContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="温度" prop="temperature">
          <el-input v-model="form.temperature" placeholder="请输入温度" />
        </el-form-item>
        <el-form-item label="电导率 (uS/cm)" prop="electricalConductivity">
          <el-input v-model="form.electricalConductivity" placeholder="请输入电导率 (uS/cm)" />
        </el-form-item>
        <el-form-item label="ph值" prop="ph">
          <el-input v-model="form.ph" placeholder="请输入ph值" />
        </el-form-item>
        <el-form-item label="氮含量估算值" prop="nitrogenEstimate">
          <el-input v-model="form.nitrogenEstimate" placeholder="请输入氮含量估算值" />
        </el-form-item>
        <el-form-item label="磷含量估算值" prop="phosphorusEstimate">
          <el-input v-model="form.phosphorusEstimate" placeholder="请输入磷含量估算值" />
        </el-form-item>
        <el-form-item label="钾含量估算值" prop="potassiumEstimate">
          <el-input v-model="form.potassiumEstimate" placeholder="请输入钾含量估算值" />
        </el-form-item>
        <el-form-item label="设备id" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备id" />
        </el-form-item>
        <el-form-item label="GPS定位经度" prop="gpslocE">
          <el-input v-model="form.gpslocE" placeholder="请输入GPS定位经度" />
        </el-form-item>
        <el-form-item label="GPS定位维度" prop="gpslocN">
          <el-input v-model="form.gpslocN" placeholder="请输入GPS定位维度" />
        </el-form-item>
        <el-form-item label="设备内存储的数据序号" prop="dataSeqid">
          <el-input v-model="form.dataSeqid" placeholder="请输入设备内存储的数据序号" />
        </el-form-item>
        <el-form-item label="sim卡号" prop="deviceIccid">
          <el-input v-model="form.deviceIccid" placeholder="请输入sim卡号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceInfo, getDeviceInfo, delDeviceInfo, addDeviceInfo, updateDeviceInfo } from "@/api/deviceInfo/deviceInfo";

export default {
  name: "DeviceInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备信息表格数据
      deviceInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // sim卡号时间范围
      daterangeNetworkTime: [],
      // sim卡号时间范围
      daterangeLocationTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceImei: null,
        networkTime: null,
        locationTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {

    this.getList();
  },
  methods: {
    /** 查询设备信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeNetworkTime && '' != this.daterangeNetworkTime) {
        this.queryParams.params["beginNetworkTime"] = this.daterangeNetworkTime[0];
        this.queryParams.params["endNetworkTime"] = this.daterangeNetworkTime[1];
      }
      if (null != this.daterangeLocationTime && '' != this.daterangeLocationTime) {
        this.queryParams.params["beginLocationTime"] = this.daterangeLocationTime[0];
        this.queryParams.params["endLocationTime"] = this.daterangeLocationTime[1];
      }
      listDeviceInfo(this.queryParams).then(response => {
        this.deviceInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceImei: null,
        batteryLevel: null,
        solarVoltage: null,
        lbslocE: null,
        lbslocN: null,
        networkTime: null,
        locationTime: null,
        moistureContent: null,
        temperature: null,
        electricalConductivity: null,
        ph: null,
        nitrogenEstimate: null,
        phosphorusEstimate: null,
        potassiumEstimate: null,
        spectrumValues: null,
        deviceId: null,
        gpslocE: null,
        gpslocN: null,
        dataSeqid: null,
        deviceIccid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeNetworkTime = [];
      this.daterangeLocationTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDeviceInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeviceInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备信息编号为"' + ids + '"的数据项？').then(function() {
        return delDeviceInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('deviceInfo/deviceInfo/export', {
        ...this.queryParams
      }, `deviceInfo_${new Date().getTime()}.xlsx`)
    },
    //隐藏左边上面导航栏
    hideElements() {
      // 先判断元素是否存在，再操作
      const navbar = document.querySelector('.navbar');
      if (navbar) { // 只有元素存在时才执行
        navbar.style.display = "none";
      }

      const tagsView = document.querySelector('.tags-view-container');
      if (tagsView) {
        tagsView.style.display = "none";
      }
    },
    mounted() {
      this.hideElements();
    },
  }
};
</script>
